﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Threading;
using HeadCounter.Armourer;
using HeadCounter.Armourer.Base;
using MathematicallySafe.HeadCounter.Commands;
using MathematicallySafe.HeadCounter.Data;
using MathematicallySafe.HeadCounter.Data.Common;
using MathematicallySafe.HeadCounter.Data.Interfaces;
using MathematicallySafe.HeadCounter.Data.Models;
using MathematicallySafe.HeadCounter.Services;
using StructureMap;

namespace MathematicallySafe.HeadCounter.ViewModels
{
    public class LootItemViewModel: ViewModelBase
    {
        private readonly ILootTable _lootTable = ObjectFactory.GetInstance<ILootTable>();

        private Dispatcher _dispatcher;

        public LootItem Model { get; private set; }

        private DelegateCommand _downloadItemCommand;
        private bool _isDownloading;

        public LootItemViewModel(LootItem model)
        {
            Model = model;
            _dispatcher = Dispatcher.CurrentDispatcher;
            Initialise();
        }

        #region Properties

        public int ID
        {
            get { return Model.ID; }
        }

        public string Name
        {
            get { return Model.Name; }
        }

        public LootSlot Slot
        {
            get { return Model.Slot; }
        }

        public int iLevel
        {
            get { return Model.iLevel; }
        }

        public string Zone
        {
            get { return Model.Zone; }
        }

        public string Boss
        {
            get { return Model.Boss; }
        }

        #endregion

        #region Armoury

        protected override void SubscribeEvents()
        {
            _lootTable.ItemDownloaded += LootTable_ItemDownloaded;
        }

        protected override void UnsubscribeEvents()
        {
            _lootTable.ItemDownloaded -= LootTable_ItemDownloaded;
        }

        void LootTable_ItemDownloaded(object sender, ItemDownloadedEventArgs e)
        {
            if (e.Id == this.ID)
            {
                Action refreshCommands = RefreshCommands;
                _dispatcher.BeginInvoke(DispatcherPriority.Normal, refreshCommands);

                OnPropertyChanged("ID");
                OnPropertyChanged("Name");
                OnPropertyChanged("Slot");
                OnPropertyChanged("iLevel");
                OnPropertyChanged("Zone");
                OnPropertyChanged("Boss");
                OnPropertyChanged("Model");
            }
        }

        private void RefreshCommands()
        {
            _isDownloading = false;
            if (_downloadItemCommand != null) _downloadItemCommand.RaiseCanExecuteChanged();
        }

        #endregion

        #region Commands

        public ICommand DownloadItemCommand
        {
            get
            {
                if (_downloadItemCommand == null)
                {
                    _downloadItemCommand = new DelegateCommand(DownloadItem, CanDownloadItem);
                }

                return _downloadItemCommand;
            }

        }

        private void DownloadItem()
        {
            _isDownloading = true;
            ObjectFactory.GetInstance<ILootTable>().DownloadItemAsynch(ID, true);
        }

        private bool CanDownloadItem()
        {
            return !_isDownloading;
        }

        #endregion
    }
}
